package com.epam.facebook_client;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.epam.facebook_client.manager.CacheManager;
import com.facebook.Session;

public class HomeActivity extends FragmentActivity {

	private ListView menuListView;

	private String[] menu = { "Albums", "Friends", "Feed" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		menuListView = (ListView) findViewById(R.id.listMenu);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplication(), R.layout.adapter_text_menu,
				R.id.textMenu, menu);
		menuListView.setAdapter(adapter);
		menuListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
				switch (position) {
				case 0:
					startActivity(new Intent(getApplication(), AlbumsActivity.class));
					break;
				case 1:
					startActivity(new Intent(getApplication(), FriendsActivity.class));
					break;
				case 2:
					startActivity(new Intent(getApplication(), ListFeedActivity.class));
					break;
				default:
					break;
				}
			}
		});

		// ((HeadFragment)getSupportFragmentManager().findFragmentById(R.id.)).load(id);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_home, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {

		switch (item.getItemId()) {
		case R.id.menu_exit:
			Session.getActiveSession().closeAndClearTokenInformation();
			startActivity(new Intent(this, StartActivity.class));
			finish();
			break;
		case R.id.menu_refresh:
			startActivity(new Intent(this, HomeActivity.class));
			finish();
			break;
		case R.id.menu_clear_cache:
			CacheManager cacheManager = CacheManager.get(this);
			Toast.makeText(this, cacheManager.clearCache() + " files deleted from the cache", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}
}
